package main

import "fmt"

/**
基本知识请参考 https://www.runoob.com/go/go-slice.html

*/

func printSlice(x []int) {
	fmt.Printf("\tlen=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
func main() {
	fmt.Println("**********slice知识点**********")

	fmt.Println("a、关于切片容量扩容问题：")
	numbers1 := make([]int, 0, 5)
	numbers1 = append(numbers1, 11)
	numbers1 = append(numbers1, 11)
	numbers1 = append(numbers1, 11)
	numbers1 = append(numbers1, 11)
	numbers1 = append(numbers1, 11)
	fmt.Println("  切片容量满了")
	printSlice(numbers1)
	numbers1 = append(numbers1, 22)
	fmt.Println("  添加超出容量范围的元素：22", "容量会自动扩到原来的2倍,长度是当前元素的个数")
	printSlice(numbers1)

	fmt.Println("b、切片的使用：")
	/* 创建切片 */
	numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
	fmt.Println(numbers)

	fmt.Println("/* 打印原始切片 */")
	fmt.Println("numbers ==", numbers)

	fmt.Println("/* 打印子切片从索引1(包含) 到索引4(不包含)*/")
	fmt.Println("numbers[1:4] ==", numbers[1:4])

	fmt.Println("/* 打印子切片从索引  0(包含) 到索引 2(不包含) */")
	number2 := numbers[:2]
	fmt.Println(number2)

	fmt.Println("/* 打印子切片从索引 2(包含) 到索引 5(不包含) */")
	number3 := numbers[2:5]
	fmt.Println(number3)

}
